package com.test.daily.leetcode.y2022.m01.m03.day0329;

/**
 * MaxConsuservation
 *
 * @author Tom on 2022/3/29
 */
public class MaxConsuservation {
    public int maxConsecutiveAnswers(String answerKey, int k){
        return Math.max(maxConscutiveChar(answerKey, k, 'T'), maxConscutiveChar(answerKey, k, 'F'));
    }

    public int maxConscutiveChar(String answerKey, int k, char ch){
        int n = answerKey.length();
        int ans = 0;
        for(int left = 0, right = 0, sum = 0; right < n; right++){
            sum+= answerKey.charAt(right) != ch ? 1: 0;
            while(sum > k){
                sum -= answerKey.charAt(left++) != ch ? 1: 0;
            }
            ans = Math.max(ans, right - left + 1);
        }
        return ans;
    }
}
